//
// Created by Scave on 2025/8/21.
//

#ifndef RIDER3D_INTERNALCOMPONENT_H
#define RIDER3D_INTERNALCOMPONENT_H

#include <utility>

#include "Define.h"
#include "render/RenderNode.h"

NS_RIDER_BEGIN

    struct LayerRenderNodes {
        Mat4 mtx_view;
        Mat4 mtx_proj;
        std::vector<SMART_PTR<RenderNode>> nodes;

        void AddNode(const SMART_PTR<RenderNode>& node) {
            nodes.push_back(node);
        }

        void Sort(FUNCTION<int(SMART_PTR<RenderNode>&, SMART_PTR<RenderNode>&)> cmp) {
            std::sort(nodes.begin(), nodes.end(), std::move(cmp));
        }

        void Clear() {
            nodes.clear();
        }
    };

    struct RenderNodes {
        HASHMAP<LayerMask, LayerRenderNodes> data;

        void SetLayerCamera(const LayerMask mask, const Mat4& view, const Mat4& proj) {
            if (data.find(mask) == data.end()) {
                data[mask] = LayerRenderNodes();
            }
            data[mask].mtx_view = view;
            data[mask].mtx_proj = proj;
        }

        void PushNode(const LayerMask mask, const SMART_PTR<RenderNode>& node) {
            if (data.find(mask) == data.end()) {
                data[mask] = LayerRenderNodes();
            }
            data[mask].AddNode(node);
        }

        void Reset() {
            for (auto& item: data) {
                item.second.Clear();
            }
            data.clear();
        }
    };

NS_RIDER_END

#endif //RIDER3D_INTERNALCOMPONENT_H
